package com.osi.mes.qm.services;

import java.util.Vector;

import pnuts.lang.Context;

import com.datasweep.compatibility.client.ATHandler;
import com.datasweep.compatibility.client.ATRow;
import com.datasweep.compatibility.client.ATRowFilter;
import com.datasweep.compatibility.client.DatasweepException;
import com.datasweep.compatibility.ui.Time;
import com.osi.mes.qm.base.constants.IATSampleChk;
import com.osi.mes.qm.base.constants.IBasicConstants;
import com.osi.mes.utils.StringUtils;

public class QmSampleChkService extends QmChkTbService implements IATSampleChk {

	public QmSampleChkService(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public Vector<ATRow> searchSampleChkRec(Time startTime, Time endTime)
			throws DatasweepException {
		ATHandler atHandler = getFunctions().createATHandler(AT_SAMPLE_CHK);
		ATRowFilter atRowFilter = getFunctions().createATRowFilter(
				AT_SAMPLE_CHK);
		if (startTime != null) {
//			atRowFilter.forColumnNameGreaterThanOrEqualTo(
//					AT_SAMPLE_CHK_SAMPLE_TIME, getDayStart(startTime));
			atRowFilter.forCreatedTimeGreaterThanOrEqualTo(getDayStart(startTime));
		}

		if (endTime != null) {
//			atRowFilter.forColumnNameLessThan(AT_SAMPLE_CHK_SAMPLE_TIME,
//					getDayStart(endTime.addDays(1)));
			atRowFilter.forCreatedTimeLessThan(getDayStart(endTime.addDays(1)));
		}

		Vector<ATRow> atRows = atHandler.getATRowsByFilter(atRowFilter, false);
		for (int i = 0; i < atRows.size(); i++) {
			ATRow atRow = atRows.get(i);
			atRow = makeChkItem(atRow);
		}

		return atRows;
	}

	public ATRow makeChkItem(ATRow atRow) {
		String msgName = IBasicConstants.SAMPLE_RESULT_MSG;
		String chkItem = "";
		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_MAT_MIC)) {
			chkItem = chkItem
					+ getFunctions().getMessage(msgName, "MAT_MICROBE");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_MAT_ST_PHYCHE)) {
			chkItem = chkItem
					+ getFunctions().getMessage(msgName, "MAT_ST_PHYCHE");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_PROD_MIC)) {
			chkItem = chkItem
					+ getFunctions().getMessage(msgName, "PROD_MICROBE");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_MAT_ALL_PHYCHE)) {
			chkItem = chkItem
					+ getFunctions().getMessage(msgName, "MAT_ALL_PHYCHE");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_WATER_MIC)) {
			chkItem = chkItem
					+ getFunctions().getMessage(msgName, "WATER_MICROBE");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_PROD_PHYCHE)) {
			chkItem = chkItem
					+ getFunctions().getMessage(msgName, "PROD_PHYCHE");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_FOOD_SURFACE_MIC)) {
			chkItem = chkItem
					+ getFunctions().getMessage(msgName, "FOOD_CON_SURFACE");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_WATER_PHYCHE)) {
			chkItem = chkItem
					+ getFunctions().getMessage(msgName, "WATER_PHYCHE");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_PWATER_MIC)) {
			chkItem = chkItem
					+ getFunctions().getMessage(msgName, "PWATER_MICROBE");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_PWATER_PHYCHE)) {
			chkItem = chkItem
					+ getFunctions().getMessage(msgName, "PWATER_PHYCHE");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_PROD_ALL)) {
			chkItem = chkItem + getFunctions().getMessage(msgName, "PROD_ALL");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_ENVIR_LIS)) {
			chkItem = chkItem
					+ getFunctions().getMessage(msgName, "ENV_LISTER");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_BBQ_ASS)) {
			chkItem = chkItem + getFunctions().getMessage(msgName, "BBQ_ASS");
			if (!StringUtils.isNull(chkItem))
				chkItem = chkItem + IBasicConstants.STRING_CONNECTOR;
		}

		if ((Boolean) atRow.getValue(AT_SAMPLE_REC_NEED_OTHER)) {
			chkItem = chkItem + atRow.getValue(AT_SAMPLE_REC_NEED_OTHER_ITEM);
		}

		if (chkItem.endsWith(IBasicConstants.STRING_CONNECTOR))
			chkItem = chkItem.substring(0,
					chkItem.lastIndexOf(IBasicConstants.STRING_CONNECTOR));

		if (!StringUtils.isNull(chkItem))
			atRow.setValue(AT_SAMPLE_REC_CHK_ITEM, chkItem);

		Long state = (Long) atRow.getValue(AT_SAMPLE_REC_STATE);
		String stateDesc = "";
		if (state.intValue() == IBasicConstants.SAMPLE_WATING_CONFIRM)
			stateDesc = getFunctions().getMessage(msgName,
					IBasicConstants.SAMPLE_NO_CONFIRM_MSG_ID);
		else
			stateDesc = getFunctions().getMessage(msgName,
					IBasicConstants.SAMPLE_CONFIRMED_MSG_ID);

		atRow.setValue(AT_SAMPLE_REC_STATE_DESC, stateDesc);
		return atRow;
	}

	public Object getChkItemMark(ATRow atRow, String chkItem) {
		if (atRow == null || StringUtils.isNull(chkItem))
			return null;

		String msgName = "QM_SamplingChk";
		if (chkItem.equals(getFunctions().getMessage(msgName, "MAT_MICROBE"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_MAT_MIC_MARK);
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "MAT_ST_PHYCHE"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_MAT_ST_PHYCHE_MARK);
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "PROD_MICROBE"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_PROD_MIC_MARK);
		}

		if (chkItem
				.equals(getFunctions().getMessage(msgName, "MAT_ALL_PHYCHE"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_MAT_ALL_PHYCHE_MARK);
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "WATER_MICROBE"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_WATER_MIC_MARK);
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "PROD_PHYCHE"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_PROD_PHYCHE_MARK);
		}

		if (chkItem.equals(getFunctions().getMessage(msgName,
				"FOOD_CON_SURFACE"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_FOOD_SURFACE_MIC_MARK);
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "WATER_PHYCHE"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_WATER_PHYCHE_MARK);
		}

		if (chkItem
				.equals(getFunctions().getMessage(msgName, "PWATER_MICROBE"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_PWATER_MIC_MARK);
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "PWATER_PHYCHE"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_PWATER_PHYCHE_MARK);
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "PROD_ALL"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_PROD_ALL_MARK);
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "ENV_LISTER"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_ENVIR_LIS_MARK);
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "ENV_LISTER"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_ENVIR_LIS_MARK);
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "BBQ_ASS"))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_BBQ_ASS_MARK);
		}

		if (chkItem.equals(atRow.getValue(AT_SAMPLE_REC_NEED_OTHER_ITEM))) {
			return atRow.getValue(AT_SAMPLE_REC_NEED_OTHER_CHK_ITEM_MARK);
		}

		return null;
	}

	public String getMsgIDFromText(String chkItem) {
		String msgName = "QM_SamplingChk";
		if (chkItem.equals(getFunctions().getMessage(msgName, "MAT_MICROBE"))) {
			return "MAT_MICROBE";
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "MAT_ST_PHYCHE"))) {
			return "MAT_ST_PHYCHE";
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "PROD_MICROBE"))) {
			return "PROD_MIRCOBE";
		}

		if (chkItem
				.equals(getFunctions().getMessage(msgName, "MAT_ALL_PHYCHE"))) {
			return "MAT_ALL_PHYCHE";
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "WATER_MICROBE"))) {
			return "WATER_MICROBE";
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "PROD_PHYCHE"))) {
			return "PROD_PHYCHE";
		}

		if (chkItem.equals(getFunctions().getMessage(msgName,
				"FOOD_CON_SURFACE"))) {
			return "FOOD_CON_SURFACE";
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "WATER_PHYCHE"))) {
			return "WATER_PHYCHE";
		}

		if (chkItem
				.equals(getFunctions().getMessage(msgName, "PWATER_MICROBE"))) {
			return "PWATER_MICROBE";
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "PWATER_PHYCHE"))) {
			return "PWATER_PHYCHE";
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "PROD_ALL"))) {
			return "PROD_ALL";
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "ENV_LISTER"))) {
			return "ENV_LISTER";
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "ENV_LISTER"))) {
			return "ENV_LISTER";
		}

		if (chkItem.equals(getFunctions().getMessage(msgName, "BBQ_ASS"))) {
			return "BBQ_ASS";
		}

		return chkItem;
	}

	public Vector<ATRow> getChkResutls(String sampleRecKey)
			throws DatasweepException {
		String atDefName = AT_SAMPLE_CHK_RESULT;
		ATHandler atHandler = getFunctions().createATHandler(atDefName);
		ATRowFilter atRowFilter = getFunctions().createATRowFilter(atDefName);
		if (!StringUtils.isNull(sampleRecKey))
			atRowFilter.forColumnNameEqualTo(AT_SAMPLE_CHK_RES_SAMPLE_REC_KEY,
					sampleRecKey);

		return atHandler.getATRowsByFilter(atRowFilter, false);
	}
	
	//通过打印的serial number 去查询取样单
	public ATRow getSampleChkTbBySerialNum(String serialNum)throws DatasweepException
	{
		String atDefName = AT_SAMPLE_CHK;
		
		ATHandler atHandler = getFunctions().createATHandler(atDefName);
		ATRowFilter atRowFilter = getFunctions().createATRowFilter(atDefName);
		if (!StringUtils.isNull(serialNum))
			atRowFilter.forColumnNameEqualTo("serial_number",
					serialNum);
		
		Vector<ATRow> atRows = super.getATRows(atRowFilter);
		if(atRows == null || atRows.size() == 0)
			return null;
		
		return atRows.get(0);
	}

}
